import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/LoginPage.vue'),
    },
    {
      path: '/',
      name: 'layout',
      component: () => import('@/views/layout/LayoutContainer.vue'),
      redirect: '/talk',
      children: [
        {
          path: '/talk',
          name: 'Layout1',
          component: () => import('@/views/talk/TalkPage.vue'),
        },
        {
          path: '/agent',
          name: 'Layout2',
          component: () => import('@/views/agent/AgentPage.vue'),
        },
        {
          path: '/study-room',
          name: 'Layout3',
          component: () => import('@/views/study-room/StudyRoomPage.vue'),
        },
        {
          path: '/instructions',
          name: 'Layout4',
          component: () => import('@/views/instructions/InstructionsPage.vue'),
        },
      ],
    },
  ],

})
// 路由守卫,拦截没有token的用户访问其他路由
router.beforeEach((to, from, next) => {
  //to: 即将进入的路由
  //from: 即将离开的路由
  //next: 放行函数
  //如果用户没有登录，则跳转到登录页
  if (to.path !== '/login' && !localStorage.getItem('user')) {
    ElMessage.warning('请先登录')
    next('/login')
  } else {
    next()
  }
})

export default router
